package Link;


public class LinkedList<E> {


    private class Node {
        public E e;

        public Node next;

        public Node(E e, Node node) {
            this.e = e;
            this.next = node;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }


    private Node dummyHead;

    private int size;

    public LinkedList() {
        this.dummyHead = new Node(null, null);
        this.size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int getSize() {
        return size;
    }


    //在链表头增加一个新的元素
    public void addFirst(E e) {
//        Node node = new Node(e);
//        node.next = dummyHead;
//        dummyHead = node;
//        size++;

        add(0, e);
    }


    //在链表的index (0--based) 位置添加一个新的元素e
    //关键要找到添加节点的钱一个节点
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add Failed,Index is Invalid!!");
        }
//        if (index == 0) {
//            addFirst(e);
//        } else {
        Node prev = dummyHead;

        //链表添加元素
        for (int i = 0; i < index; i++)
            prev = prev.next;

        Node node = new Node(e);
        node.next = prev.next;
        prev.next = node;

        size++;
//        }
    }


    //在最末尾添加一个元素
    public void addLast(E e) {
        add(size, e);
    }


    //获取链表的第index 位置元素
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Add Failed,Index is Invalid!!");
        }
        Node current = dummyHead.next;
        for (int i = 0; i < index - 1; i++)
            current = current.next;
        return current.e;
    }

    //获取链表的第一个值
    public E getFirst() {
        return get(0);
    }

    //获取链表的最后一个值
    public E getLast() {
        return get(size - 1);
    }

    //设置元素的值
    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Add Failed,Index is Invalid!!");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++)
            cur = cur.next;
        cur.e = e;
    }

    //链表中是否包含某一个元素
    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while (cur != null) {
            if (cur.e.equals(e)) {
                return true;
            }
            //查看到下一个节点
            cur = cur.next;
        }
        return false;
    }


    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Add Failed,Index is Invalid!!");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node node = prev.next;

        prev.next = node.next;
        node.next = null;
        size--;
        return node.e;

    }


    //删除第一个元素
    public E removeFirst() {
        return remove(0);
    }

    //链表中删除最后一个元素
    public E removeLast() {
        return remove(size - 1);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null) {
            res.append(cur + "==>");
            cur = cur.next;
        }
        res.append(" NULL ");
        return res.toString();
    }
}
